package com.unlcn.ils.kas.controller.order;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.unlcn.ils.kas.dao.model.order.OrderOtd;
import com.unlcn.ils.kas.dao.model.order.bo.OrderSelectBo;
import com.unlcn.ils.kas.service.order.ExcelService;
import com.unlcn.ils.kas.service.util.ExcelUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * excel 相关接口
 * 
 * @author 罗必量
 */
@Controller
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    /***
     * 获取excel数据
     * 
     * @return 返回文件名称及excel文件的URL
     * @throws IOException
     */

    @RequestMapping("/getExcel")
    public void getExcel(OrderSelectBo orderSelectBo, HttpServletResponse response) throws IOException {

        String[] titles = { "中联订单号", "客户订单号", "客户名称", "订单日期", "车架号", "车型代码", "车型名称", "经销商名称", "发车省", "发车市", "发车地址",
                "收车省", "收车市", "收车地址", "运输距离", "订单状态", "运输方式", "接单时间", "提车时间", "打单时间", "入中联库时间", "安排运力时间", "出中联库时间",
                "入铁路库时间", "装车时间", "离昌时间", "承运车队名称", "司机联系方式", "车牌号", "预计送达时间", "送达时间", "回单时间", "发运类型", "最新在途位置",
                "最新在途时间", "备注", "备注说明" };

        // 查询导出数据
        List<OrderOtd> list = excelService.export(orderSelectBo);

        // excel映射
        Map<String, Object> mappering = new HashMap<>();
        mappering.put("sysOrderNo", "中联订单号");
        mappering.put("customerOrderNo", "客户订单号");
        mappering.put("customerName", "客户名称");
        mappering.put("orderDate", "订单日期");
        mappering.put("vin", "车架号");
        mappering.put("styleCode", "车型代码");
        mappering.put("styleName", "车型名称");
        mappering.put("dealerName", "经销商名称");
        mappering.put("oProvince", "发车省");
        mappering.put("oCity", "发车市");
        mappering.put("oAddress", "发车地址");
        mappering.put("dProvince", "收车省");
        mappering.put("dCity", "收车市");
        mappering.put("dAddress", "收车地址");
        mappering.put("distance", "运输距离");
        mappering.put("oAddress", "发车地址");
        mappering.put("oAddress", "发车地址");
        mappering.put("orderStatus", "订单状态");
        mappering.put("transportType", "运输方式");
        mappering.put("orderCreateTime", "接单时间");
        mappering.put("pickTime", "提车时间");
        mappering.put("printOrderTime", "打单时间");
        mappering.put("inboundTime", "入中联库时间");
        mappering.put("arrangeCapacityTime", "安排运力时间");
        mappering.put("outboundTime", "出中联库时间");
        mappering.put("enterRailwayTime", "入铁路库时间");
        mappering.put("loadTime", "装车时间");
        mappering.put("leaveTime", "离昌时间");
        mappering.put("supplier", "承运车队名称");
        mappering.put("driverPhone", "司机联系方式");
        mappering.put("licensePlate", "车牌号");
        mappering.put("planShiptoTime", "预计送达时间");
        mappering.put("shiptoTime", "送达时间");
        mappering.put("returnOrderTime", "回单时间");
        mappering.put("shipmentType", "发运类型");
        mappering.put("newestPosition", "最新在途位置");
        mappering.put("newestTime", "最新在途时间");
        mappering.put("remark", "备注");
        mappering.put("remarkDesc", "备注说明");

        // 导出
        HSSFWorkbook excel = ExcelUtil.export("订单", titles, list, mappering);

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=order.xls");
        OutputStream ouputStream = response.getOutputStream();
        excel.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }

    /***
     * 获取异常订单 excel 数据
     * 
     * @return 返回文件名称及excel文件的URL
     * @throws IOException
     */
    @RequestMapping("/getExceptionExcel")
    public void getExceptionExcel(HttpServletResponse response, Integer customerNo, Integer classCode)
            throws IOException {

        String[] titles = { "中联订单号", "客户订单号", "客户名称", "订单日期", "车架号", "车型代码", "车型名称", "经销商名称", "发车省", "发车市", "发车地址",
                "收车省", "收车市", "收车地址", "运输距离", "订单状态", "运输方式", "接单时间", "提车时间", "打单时间", "入中联库时间", "安排运力时间", "出中联库时间",
                "入铁路库时间", "装车时间", "离昌时间", "承运车队名称", "司机联系方式", "车牌号", "预计送达时间", "送达时间", "回单时间", "发运类型", "最新在途位置",
                "最新在途时间", "备注", "备注说明" };

        // 查询导出数据
        List<OrderOtd> list = excelService.getExceptionExcel(customerNo, classCode);

        // excel映射
        Map<String, Object> mappering = new HashMap<>();
        mappering.put("sysOrderNo", "中联订单号");
        mappering.put("customerOrderNo", "客户订单号");
        mappering.put("customerName", "客户名称");
        mappering.put("orderDate", "订单日期");
        mappering.put("vin", "车架号");
        mappering.put("styleCode", "车型代码");
        mappering.put("styleName", "车型名称");
        mappering.put("dealerName", "经销商名称");
        mappering.put("oProvince", "发车省");
        mappering.put("oCity", "发车市");
        mappering.put("oAddress", "发车地址");
        mappering.put("dProvince", "收车省");
        mappering.put("dCity", "收车市");
        mappering.put("dAddress", "收车地址");
        mappering.put("distance", "运输距离");
        mappering.put("oAddress", "发车地址");
        mappering.put("oAddress", "发车地址");
        mappering.put("orderStatus", "订单状态");
        mappering.put("transportType", "运输方式");
        mappering.put("orderCreateTime", "接单时间");
        mappering.put("pickTime", "提车时间");
        mappering.put("printOrderTime", "打单时间");
        mappering.put("inboundTime", "入中联库时间");
        mappering.put("arrangeCapacityTime", "安排运力时间");
        mappering.put("outboundTime", "出中联库时间");
        mappering.put("enterRailwayTime", "入铁路库时间");
        mappering.put("loadTime", "装车时间");
        mappering.put("leaveTime", "离昌时间");
        mappering.put("supplier", "承运车队名称");
        mappering.put("driverPhone", "司机联系方式");
        mappering.put("licensePlate", "车牌号");
        mappering.put("planShiptoTime", "预计送达时间");
        mappering.put("shiptoTime", "送达时间");
        mappering.put("returnOrderTime", "回单时间");
        mappering.put("shipmentType", "发运类型");
        mappering.put("newestPosition", "最新在途位置");
        mappering.put("newestTime", "最新在途时间");
        mappering.put("remark", "备注");
        mappering.put("remarkDesc", "备注说明");

        // 导出
        HSSFWorkbook excel = ExcelUtil.export("订单", titles, list, mappering);

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=order.xls");
        OutputStream ouputStream = response.getOutputStream();
        excel.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}
